package exam.controller;

import exam.bean.PaperInfo;
import exam.service.IPaperInfoService;
import exam.utils.FileUtil;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController()
@RequestMapping("/PaperInfos")
public class PaperInfoController {
    @Autowired
    IPaperInfoService paperInfoService;

    @Value("${upload.uploadPath}")
    private String uploadPath;

    @PreAuthorize("hasAuthority('sys:paper:list')")
    @ResponseBody
    @GetMapping("/paperInfo")
    public Msg paperInfo(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(value = "paperName", defaultValue = "") String paperName) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("paperName", paperName);
        return Msg.success().add("pageInfo", paperInfoService.selectInfo(pageNum, pageSize, queryMap));
    }

    @PostMapping("/searchPaperInfo/{pageNum}/{pageSize}")
    public Msg searchPaperInfo(@PathVariable String pageNum,@PathVariable String pageSize,@RequestBody Map queryMap){
        return Msg.success().add("pageInfo", paperInfoService.selectInfo(Integer.parseInt(pageNum), Integer.parseInt(pageSize), queryMap));
    }


    @PreAuthorize("hasAuthority('sys:paper:add')")
    @PostMapping("/add")
    public Msg add(
            @RequestPart("paperInfo") PaperInfo paperInfo,
            @RequestPart(value = "file", required = false) MultipartFile[] file
    ) {
//        System.out.println("论文附件个数 :" + file.length);
        paperInfo.setFiles(Arrays.asList(file));
        boolean re = paperInfoService.insertInfo(paperInfo, uploadPath) > 0;
        return Msg.success().setMessage(re ? "添加成功" : "添加失败");
    }

    @PreAuthorize("hasAuthority('sys:paper:detail')")
    @ResponseBody
    @GetMapping("/detail")
    public Msg detail(@RequestParam(value = "id") int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        System.out.println("论文id" + id);
        return Msg.success().add("paperInfo", paperInfoService.selectInfoSingle(map));
    }

    @PreAuthorize("hasAuthority('sys:paper:delete')")
    @ResponseBody
    @GetMapping("/delete")
    public Msg delete(@RequestParam(value = "id") int id) {
        boolean re = paperInfoService.deleteInfo(id) > 0;
        return Msg.success().setMessage(re ? "删除成功" : "删除失败");
    }

    @PreAuthorize("hasAuthority('sys:paper:update')")
    @ResponseBody
    @PostMapping("/upData")
    public Msg update(@RequestPart("paperInfo") PaperInfo paperInfo,
                      @RequestPart(value = "files",required = false) MultipartFile[] files) {
        paperInfo.setFiles(Arrays.asList(files));
        System.out.println(paperInfo);
        boolean re = paperInfoService.updateInfo(paperInfo,uploadPath) > 0;
        return Msg.success().setMessage(re ? "更新成功" : "更新失败");
    }

    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response, String fileName, String fileNameSrc) {
        FileUtil.downLoadFile(response, uploadPath, fileName, fileNameSrc);
    }

}
